package com.ckx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ckx.constants.SystemConstants;
import com.ckx.domain.ResponseResult;
import com.ckx.domain.dto.LinkDto;
import com.ckx.domain.entity.Link;
import com.ckx.domain.vo.LinkVo;
import com.ckx.domain.vo.PageVo;
import com.ckx.enums.AppHttpCodeEnum;
import com.ckx.exception.SystemException;
import com.ckx.mapper.LinkMapper;
import com.ckx.service.LinkService;
import com.ckx.utils.BeanCopyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Objects;

@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Override
    public List<LinkVo> getAllLink() {
        //查询所有审核通过的
        QueryWrapper<Link> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL);//审核通过
        List<Link> links = list(wrapper);
        //转换成vo
        List<LinkVo> linkVos = BeanCopyUtil.copyBeanList(links, LinkVo.class);
        //封装返回
        return linkVos;
    }

    @Override
    public PageVo listByPage(Integer pageNum, Integer pageSize, Link link) {
        QueryWrapper<Link> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(StringUtils.hasText(link.getName()), Link::getName, link.getName());
        wrapper.lambda().eq(StringUtils.hasText(link.getStatus()), Link::getStatus, link.getStatus());

        Page<Link> page = new Page<>(pageNum, pageSize);
        page(page, wrapper);
        List<LinkVo> linkVos = BeanCopyUtil.copyBeanList(page.getRecords(), LinkVo.class);

        return new PageVo(linkVos, page.getTotal());
    }

    @Override
    public void addLink(LinkDto linkDto) {
        Link link = BeanCopyUtil.copyBean(linkDto, Link.class);
        this.save(link);
    }

    @Override
    public void del4DelFlag(List<Long> ids) {

        UpdateWrapper<Link> wrapper = new UpdateWrapper<>();
        wrapper.lambda().set(Link::getDelFlag, SystemConstants.DELFLAG);
        wrapper.lambda().in(Link::getId, ids);
        this.update(wrapper);

    }

    @Override
    public LinkVo getLinkInfo(Long id) {
        if (null == id || id <= 0) {
            throw new SystemException(AppHttpCodeEnum.ARGS_FAIL);
        }
        Link link = this.getById(id);
        if (Objects.isNull(link)) {
            throw new SystemException(AppHttpCodeEnum.ARGS_FAIL);
        }
        return BeanCopyUtil.copyBean(link, LinkVo.class);
    }

    @Override
    public void updateLink(LinkDto linkDto) {
        Link link = BeanCopyUtil.copyBean(linkDto, Link.class);
        UpdateWrapper<Link> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(Link::getId, link.getId());
        this.update(link, wrapper);
    }

    @Override
    public void changeLinkStatus(LinkDto linkDto) {
        UpdateWrapper<Link> wrapper = new UpdateWrapper<>();
        wrapper.lambda().set(Link::getStatus, linkDto.getStatus());
        wrapper.lambda().eq(Link::getId, linkDto.getId());
        this.update(wrapper);
    }


}
